#include "stdafx.h"
#include "Prog2.h"

#include "Prog2Doc.h"
#include "ZeroInit.h"
#include "GridParam.h"
#include "Prog2View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


IMPLEMENT_DYNCREATE(CProg2Doc, CDocument)

BEGIN_MESSAGE_MAP(CProg2Doc, CDocument)
END_MESSAGE_MAP()


// CProg2Doc construction/destruction

CProg2Doc::CProg2Doc()
: m_TrajectoryBuilder(this)
{
}

CProg2Doc::~CProg2Doc()
{
}

BOOL CProg2Doc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	try
	{
		m_TrajectoryBuilder.Open();
		m_TrajectoryBuilder.Calc(this, NULL);
	}
	catch(const CCustomException &e)
	{
		AfxMessageBox(e.GetErrorStr());
		return FALSE;
	}

	return TRUE;
}

BOOL CProg2Doc::OnOpenDocument(LPCTSTR lpszPathName)
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;

	try
	{
		m_TrajectoryBuilder.Open(lpszPathName);
		m_TrajectoryBuilder.Calc(this, NULL);
	}
	catch(const CCustomException &e)
	{
		AfxMessageBox(e.GetErrorStr());
		return FALSE;
	}
	return TRUE;
}


// CProg2Doc serialization

void CProg2Doc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}


// CProg2Doc diagnostics

#ifdef _DEBUG
void CProg2Doc::AssertValid() const
{
	CDocument::AssertValid();
}

void CProg2Doc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

bool CProg2Doc::OnError(LPCTSTR stzError)
{
	if(stzError)
		AfxMessageBox(stzError);
	return true;
}

bool CProg2Doc::AppendTraceApproaches(CApproachTrace::Ptr listApprc)
{
	bool bRc = m_ApproachHolder.AppendTraceApproaches(listApprc);

	if(bRc)
	{
		POSITION pos = GetFirstViewPosition();
		while (pos != NULL)
		{
			CView* pView = GetNextView(pos);
			CProg2View *pProgView = dynamic_cast<CProg2View *>(pView);
			if(pProgView)
			{
				pProgView->AppendTraceApproaches(listApprc);
			}
		}
	}
	return bRc;
}

bool CProg2Doc::GetApproachesInRange(const CGridParam &ranges, ListPtrApproachs &ListOut)
{
	return m_ApproachHolder.GetApproachesInRange(ranges, ListOut);
}

bool CProg2Doc::GetInitialGridParam(CGridParam & gridParam)
{
	return m_TrajectoryBuilder.GetRefRange(&gridParam);
}

